/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package intelligenttm;

import java.io.IOException;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;


//import org.apache.batik.anim.dom.SAXSVGDocumentFactory;
import org.apache.batik.bridge.BridgeContext;
import org.apache.batik.bridge.GVTBuilder;
import org.apache.batik.bridge.UserAgentAdapter;
import org.apache.batik.ext.awt.RenderingHintsKeyExt;
import org.apache.batik.gvt.GraphicsNode;
import org.apache.batik.util.XMLResourceDescriptor;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.svg.SVGDocument;

import org.w3c.dom.Document;



import org.apache.batik.dom.svg.*;



/**
 *
 * @author 望舒凌霄
 */
public class SVGImage {
    private  GraphicsNode rootSvgNode;
    private  SVGDocument svgDocument;
    
    public SVGImage()  {
        svgDocument = null;
        rootSvgNode = null;
    }
    
    public SVGImage(String string) {
        try {
            String parser = XMLResourceDescriptor.getXMLParserClassName();
            SAXSVGDocumentFactory SAXSVGDF = new SAXSVGDocumentFactory(parser);
            Document doc = SAXSVGDF.createDocument(string);
            svgDocument =(SVGDocument) doc;
            rootSvgNode = getRootNode(svgDocument);
        } catch (IOException ex) {
            // ...
        }

    }
    
    public SVGImage(SVGDocument document) {
        svgDocument = document;
        rootSvgNode = getRootNode(svgDocument);
    }
    
    public SVGImage(byte[] svgXml){
        InputStream is = new ByteArrayInputStream(svgXml); 
        String parser = XMLResourceDescriptor.getXMLParserClassName();
        SAXSVGDocumentFactory SAXSVGDF = new SAXSVGDocumentFactory(parser);
        Document doc=null;
        try {
            doc = SAXSVGDF.createDocument(null,is);
        } catch (IOException ex) {
            Logger.getLogger(ITMGUIMainController.class.getName()).log(Level.SEVERE, null, ex);
        }
        svgDocument =(SVGDocument) doc;
        rootSvgNode = getRootNode(svgDocument);
    }
    
    static public Node getRoot(String file) throws IOException{
    	String parser = XMLResourceDescriptor.getXMLParserClassName();
        SAXSVGDocumentFactory factory = new SAXSVGDocumentFactory(parser);
        return factory.createDocument(file);
 	
    }
    
     private static GraphicsNode getRootNode(SVGDocument document) {
        UserAgentAdapter userAgentAdapter = new UserAgentAdapter();
        BridgeContext bridgeContext = new BridgeContext(userAgentAdapter);
        GVTBuilder builder = new GVTBuilder();

        return builder.build(bridgeContext, document);
    }
     
     public void setSvgDocument(SVGDocument document) {
        svgDocument = document;
        rootSvgNode = getRootNode(svgDocument);
    }
    
     public SVGDocument getSvgDocument() {
        return svgDocument;
    }
     
     public GraphicsNode getRootSvgNode() {
        return rootSvgNode;
    }
     
     public Image getImage(int width, int height) {
         
        Element elt = svgDocument.getRootElement();
        Rectangle2D bounds = rootSvgNode.getPrimitiveBounds();
        double scaleX, scaleY;
        if(width <= 0){
            scaleX = scaleY = height/bounds.getHeight();
            width = (int) (scaleX * bounds.getWidth());
        }else if(height <= 0){
            scaleX = scaleY = width/bounds.getWidth();
            height = (int) (scaleY * bounds.getHeight());
        }else{
            scaleX = width/bounds.getWidth();
            scaleY = height/bounds.getHeight();
        }
        
        // Paint svg into image buffer
        BufferedImage bufferedImage = new BufferedImage(width,
                height, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2d = (Graphics2D) bufferedImage.getGraphics();

        // For a smooth graphic with no jagged edges or rastorized look.
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        
        g2d.setRenderingHint(RenderingHintsKeyExt.KEY_TRANSCODING,
                RenderingHintsKeyExt.VALUE_TRANSCODING_PRINTING);

        // Scale image to desired size
        AffineTransform usr2dev =
                new AffineTransform(scaleX, 0.0, 0.0, scaleY, 0.0, 0.0);
        g2d.transform(usr2dev);
        rootSvgNode.paint(g2d);

        // Cleanup and return image
        g2d.dispose();
        return bufferedImage;
    }
     
//     public void SaveAsJPEG(String OutPath) throws MalformedURLException, FileNotFoundException, TranscoderException, IOException{
//         // Create a JPEG transcoder
//        JPEGTranscoder t = new JPEGTranscoder();
//
//        // Set the transcoding hints.
//        t.addTranscodingHint(JPEGTranscoder.KEY_QUALITY,new Float(.8));
//        // Create the transcoder input.
//        OutPath="newFace.jpg";
//        File OutFile = new File(OutPath);
//        //TranscoderInput input = new TranscoderInput(svgURI);
//        
//        TranscoderInput input = new TranscoderInput(svgDocument);
//        
//        
//        FileOutputStream ostream = new FileOutputStream(OutFile);
//        TranscoderOutput output = new TranscoderOutput(ostream);
//
//        // Save the image.
//        t.transcode(input, output);
//
//        // Flush and close the stream.
//        ostream.flush();
//        ostream.close();
//        
//     }
//     
//     
//     
//     
     
//     //svg转pdf
//     public static void convertSvgFile2Pdf(File svg, File pdf) throws IOException,TranscoderException   
//    {  
//        InputStream in = new FileInputStream(svg);  
//        OutputStream out = new FileOutputStream(pdf);  
//        out = new BufferedOutputStream(out);  
//        convert2Pdf(in, out);  
//    }  
//     
//    public static void convert2Pdf(InputStream in, OutputStream out)throws IOException, TranscoderException  
//    {  
//        Transcoder transcoder = new PDFTranscoder();  
//        try {  
//            TranscoderInput input = new TranscoderInput(in);  
//            try {  
//                TranscoderOutput output = new TranscoderOutput(out);  
//                transcoder.transcode(input, output);  
//            } finally {  
//                out.close();  
//            }  
//        } finally {  
//            in.close();  
//        }  
//    }  
//    //svg转为png  
//    public static void convertSvgFile2Png(File svg, File png) throws IOException,TranscoderException   
//    {  
//        InputStream in = new FileInputStream(svg);  
//        OutputStream out = new FileOutputStream(png);  
//        out = new BufferedOutputStream(out);  
//        convert2Png(in, out);  
//    }  
//    public static void convert2Png(InputStream in, OutputStream out)throws IOException, TranscoderException  
//    {  
//        Transcoder transcoder = new PNGTranscoder();  
//        try {  
//            TranscoderInput input = new TranscoderInput(in);  
//            try {  
//                TranscoderOutput output = new TranscoderOutput(out);  
//                transcoder.transcode(input, output);  
//            } finally {  
//                out.close();  
//            }  
//        } finally {  
//            in.close();  
//        }  
//    }  
//    //字符串转成pdf  
//    public static void convertStr2Pdf(String svg, File pdf) throws IOException,TranscoderException   
//    {  
//        InputStream in = new ByteArrayInputStream(svg.getBytes());  
//        OutputStream out = new FileOutputStream(pdf);  
//        out = new BufferedOutputStream(out);  
//        convert2Pdf(in, out);  
//    } 
     
     
}
